package les.projeto;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.Context;
import android.os.Environment;

public class Export {

	private Context context;
	
	public Export(Context context){
		this.context = context;
	}
	
	public String getFromBD(){
		BD bd = new BD();
		ArrayList<String> teachers = bd.getTeachers(context);
		ArrayList<String> activities = bd.getActivityOrderByDate(context);
		ArrayList<String> local = bd.getLocal(context);
		ArrayList<String> subjects = bd.getSubjects(context);
		ArrayList<String> semester = bd.getSemester(context);
		
		String dataInBD = "";
		if (teachers.isEmpty())
			dataInBD = dataInBD + "-";
		else{
			for (int i = 0; i < teachers.size(); i++) {
				dataInBD = dataInBD + teachers.get(0).replace(" ", ".");
			}
		}
		
		dataInBD = dataInBD + "\n";
		
		if (activities.isEmpty())
			dataInBD = dataInBD + "-";
		else{
			for (int i = 0; i < activities.size(); i++) {
				dataInBD = dataInBD + activities.get(0).replace(" ", ".");
			}
		}
		
		dataInBD = dataInBD + "\n";
		
		if (local.isEmpty())
			dataInBD = dataInBD + "-";
		else{
			for (int i = 0; i < local.size(); i++) {
				dataInBD = dataInBD + local.get(0).replace(" ", ".");
			}
		}
		
		dataInBD = dataInBD + "\n";
		
		if (subjects.isEmpty())
			dataInBD = dataInBD + "-";
		else{
			for (int i = 0; i < subjects.size(); i++) {
				dataInBD = dataInBD + subjects.get(0).replace(" ", ".");
			}
		}
		
		dataInBD = dataInBD + "\n";
		
		if (semester.isEmpty())
			dataInBD = dataInBD + "-";
		else{
			for (int i = 0; i < semester.size(); i++) {
				dataInBD = dataInBD + semester.get(0).replace(" ", ".");
			}
		}
		return dataInBD;
	}
	
	public void exportData() {
		String data = getFromBD();
		try
	    {
	        File root = new File(Environment.getExternalStorageDirectory(), "Documentos");
	        System.out.println(root.getAbsolutePath());
	        if (!root.exists()) {
	            root.mkdirs();
	        }
	        File gpxfile = new File(root, "teste.txt");
	        FileWriter writer = new FileWriter(gpxfile);
	        writer.append(data);
	        writer.flush();
	        writer.close();
	    }
	    catch(IOException e)
	    {
	         e.printStackTrace();
	    }
	} 
}
